package com.school.springboot_login.controller;


import com.school.springboot_login.annotation.LogAnnotation;
import com.school.springboot_login.bean.LoginCommand;
import com.school.springboot_login.bean.User;
import com.school.springboot_login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;


@Controller
//使用 RequesrMapping映射请求
@RequestMapping(value="/")
public class LoginController {
    //自动注入
    @Autowired
    private UserService userService;

    //测试
    @RequestMapping("/hello.html")
    public String hello(){
        return "Hello Spring Boot,这里是LoginController";
    }


    @RequestMapping("/loginCheck.html")//负责处理loginCheck.html的请求
    @LogAnnotation(moduleName = "用户模块",option = "用户登录")//切点自定义注释
    public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) throws ServletException,IOException {
        //设置字符编码
        request.setCharacterEncoding("UTF-8");//防止post乱码
        //控制台输出登录用户的姓名和密码
        System.out.println(loginCommand.getUserName() + loginCommand.getPassword());
        //在service层判断是否存在这样的用户
        boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
        //缓存判断登录次数
        String loginSuccess = userService.loginService(loginCommand.getUserName(), loginCommand.getPassword());
//        if (loginSuccess != null && loginSuccess.length() != 0) {
//
//            return new ModelAndView("login", "error", loginSuccess);
//        }
//
            if ("".equals(loginCommand.getUserName()) || "".equals(loginCommand.getPassword())) {

                return new ModelAndView("login", "error", "用户名或密码为空。");
            }
           else if(loginSuccess!=null&&loginSuccess.length()!=0&&loginSuccess!="登录成功")
            {
                  User user = userService.findUserByUserName(loginCommand.getUserName());
//                userService.insertLog2(user);
                  //user.setLoginFailedTimes(1+user.getLoginFailedTimes());
//                  userService.insertLog2(user);
                  System.out.println(user.getLoginFailedTimes());
                return new ModelAndView("login", "error", loginSuccess);
            }else {
                User user = userService.findUserByUserName(loginCommand.getUserName());
                user.setLastIp(request.getLocalAddr());//获取IP地址
                user.setLastVisit(new Date());
               // user.setLoginTimes(1+user.getLoginTimes());
                userService.loginSuccess(user);
//                userService.insertLog2(user);
//                System.out.println(user.getUserName());
//                System.out.println(user.getPassword());
//                System.out.println(user.getCredits());
                System.out.println(user.getLoginTimes());

                request.getSession().setAttribute("user", user);
                return new ModelAndView("main");
            }

    }


    @RequestMapping("/return.html")//处理login.html的请求,success
    public ModelAndView loginPage(){
        return new ModelAndView("login");
    }
//    public String loginPage(){
//        return "login";
//    }
    //注册
    @RequestMapping("/insert.html")//register.jsp
    @LogAnnotation(moduleName = "用户模块",option = "用户注册")
    public ModelAndView insertUser(HttpServletRequest request, User user){
        if("".equals(user.getUserName())||"".equals(user.getPassword()))
        {

            return new ModelAndView("register", "error", "用户名或密码为空。");

        }
        else {
            userService.InsertUser(user);
            //redis
            userService.registerService(user.getUserName(), user.getPassword());
            System.out.println("用户注册成功");
        /*model.addAttribute("Insert","注册成功");
        System.out.println(model.getAttribute("Insert"));*/
            request.getSession().setAttribute("user1", user);
            //return "success";
            return new ModelAndView("success", "insert", "用户注册成功。");
        }
    }


//    public String InsertPage()
//    {
//        return "register";
//    }
    @RequestMapping("/insertPage.html")
    public ModelAndView InsertPage()
    {
        return new ModelAndView("register");
    }


    //定时采集信息
//    @Scheduled(cron = "0 19 17 * * ?")
//    public void LogTasks() {
//
//        userService.insertLog();
//
//    }
//
}
